package monkeytest;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.Date;
import source.Common;

public class HomeRefresh {
	public static String ADB_PATH = Common.ADB_PATH;;
	public static String package_name = Common.HEAD_PAGEKAGE;

	public static void main(String[] args) {
		String timess = args[0];
		int times = Common.getInstance().changeStringToInt(timess);
		for (int i = 0; i < times; i++) {
			Date start = Common.getInstance().getDate();
			Logcat logcat = new Logcat();// 新建记录log线程
			PerformanceThread performanceThread = new PerformanceThread("homerefresh", package_name);// 性能监控
			performanceThread.start();
			logcat.start();
			String command = "adb shell uiautomator runtest demo.jar --nohup -c happyjuzi.AppCase#testHomeRefresh";
			execCmdAdb(command, "homerefresh" + getNow() + ".log");
			logcat.stopLoacat();
			performanceThread.stopRecord();
			Date end = Common.getInstance().getDate();
			Common.getInstance().outputTimeDiffer(start, end);
		}
	}

	/**
	 * 执行adb命令
	 * 
	 * @param cmd
	 *            命令内容
	 * @param fileName
	 *            输入文件路径
	 */
	public static void execCmdAdb(String cmd, String fileName) {
		System.out.println("执行命令：" + cmd);
		String OSname = System.getProperty("os.name");
		Common.getInstance().saveToFile(Common.getInstance().getNow() + "开始！" + Common.LINE, fileName);// 保存
		try {
			Process p = null;
			if (OSname.contains("Mac")) {
				p = Runtime.getRuntime().exec(ADB_PATH + cmd);
			} else {
				p = Runtime.getRuntime().exec("cmd /c " + cmd);
			}
			// 正确输出流
			InputStream input = p.getInputStream();// 创建并实例化输入字节流
			BufferedReader reader = new BufferedReader(new InputStreamReader(input));// 先通过inputstreamreader进行流转化，在实例化bufferedreader，接收内容
			String line = "";
			while ((line = reader.readLine()) != null) {// 循环读取
				if (line.startsWith("//   ")) {
					continue;
				}
				if (line.startsWith("//")) {
					Common.getInstance().saveToFile(line, fileName);// 保存
				}
				if (line.contains("Exception")) {
					Common.getInstance().saveToFile("error-homerefresh" + getNow(), "error-homerefresh"+getNow()+".log");
				}
			}
			reader.close();// 此处reader依赖于input，应先关闭
			input.close();
			// 错误输出流
			InputStream errorInput = p.getErrorStream();// 创建并实例化输入字节流
			BufferedReader errorReader = new BufferedReader(new InputStreamReader(errorInput));// 先通过inputstreamreader进行流转化，在实例化bufferedreader，接收内容
			String eline = "";
			while ((eline = errorReader.readLine()) != null) {// 循环读取
				Common.getInstance().saveToFile(eline, fileName);// 保存

			}
			errorReader.close();// 此处有依赖关系，先关闭errorReader
			errorInput.close();
		} catch (IOException e) {
			Common.getInstance().output("执行" + cmd + "失败！");
			e.printStackTrace();
		}
		Common.getInstance().saveToFile(Common.LINE + Common.getInstance().getNow() + "结束！", fileName);// 保存
	}

	/**
	 * 获取当前时间
	 * 
	 * @return 返回当前时间，只有日期和小时和分数没有年份和秒数
	 */
	private static String getNow() {
		Date time = new Date();
		SimpleDateFormat now = new SimpleDateFormat("MM-dd-HH-mm");
		String c = now.format(time);
		return c;
	}

	public void output(String text) {
		System.out.println(text);
	}

	public void output(Object... object) {
		if (object.length == 1) {
			output(object[0].toString());
			return;
		}
		for (int i = 0; i < object.length; i++) {
			System.out.println("第" + (i + 1) + "个：" + object[i]);
		}
	}
}
